<template>
	<view>
		<view class="userinfoBg" v-for="(item,index) in userinfo" :key="index" @click="toSetInfo(index,item)">
			<view class="infobg">
				<view class="title">
					{{ item.title }}
				</view>
				<view class="info">
					{{ item.info }}
				</view>
				<image class="arrow" src="/static/images/setting/arrow.png" mode=""></image>
			</view>
			<view class="line">

			</view>
		</view>
		<view class="btn_bg">
			<button type="primary" @click="logoutClick">退出登录</button>
		</view>
	</view>
</template>

<script>
	import config from '../../common/config.js';
	export default {
		onShow() {
			this.getUserInfo();
		},
		onLoad() {
			setTimeout(function() {
				console.log('start pulldown');
			}, 1000);
			uni.startPullDownRefresh();
		},
		onPullDownRefresh() {
			console.log('refresh');
			this.getUserInfo();
		},
		data() {
			return {
				userinfo: new Object()
			}
		},
		methods: {
			getUserInfo() {
				let userinfoURL = config.baseURL + "info/userinfo";
				self = this;
				uni.request({
					url: userinfoURL,
					method: 'GET',
					header: {
						"Authorization": config.getToken()
					},
					success: function(res) {
						console.log(res.data);
						setTimeout(function() {
							uni.stopPullDownRefresh();
						}, 1000);
						if (res.data.isok == 1) {
							let info = res.data.data.userinfo;
							for (let key in info) {
								if (key == "nickname") {
									self.userinfo[0] = {
										"title": "昵称",
										"info": info[key]
									}
								}
								if (key == "gender") {
									self.userinfo[1] = {
										"title": "性别",
										"info": info[key] == 0 ? "男" : "女"
									}
								}
								if (key == "address") {
									self.userinfo[2] = {
										"title": "地区",
										"info": info[key]
									}
								}
								if (key == "motto") {
									self.userinfo[3] = {
										"title": "座右铭",
										"info": info[key]
									}
								}
							}
							console.log(self.userinfo)

						} else {
							console.log(res.data.error_code);
							if (res.data.error_code == 401) {
								uni.reLaunch({
									url: "/pages/login/login"
								})
							}
							uni.showToast({
								title: "用户信息获取失败",
								icon: "error"
							})
						}
					},
					fail: function(res) {
						console.log(res.errMsg.error_code);
						setTimeout(function() {
							uni.stopPullDownRefresh();
						}, 1000);
						if (res.errMsg.error_code == 401) {
							uni.reLaunch({
								url: "/pages/login/login"
							})
						}
						uni.showToast({
							title: "用户信息获取失败",
							icon: "error"
						})
					}
				})
			},
			logoutClick() {
				console.log(config.getToken());
				let logoutURL = config.baseURL + "user/logout";
				uni.request({
					url: logoutURL,
					method: 'GET',
					header: {
						"Authorization": config.getToken()
					},
					success: function(res) {
						console.log(res.data);
						if (res.data.isok == 1) {
							uni.switchTab({
								url: "/pages/index/index"
							});
							uni.removeStorage({
								key: "token",
								success: function() {}
							})
							uni.showToast({
								title: "退出成功",
								icon: "success"
							});
						} else {
							uni.showToast({
								title: "退出失败",
								icon: "error"
							})
						}
					},
					fail: function(res) {
						console.log(res.errMsg);
						uni.showToast({
							title: "退出失败",
							icon: "error"
						})
					}
				});
			},
			toSetInfo(index, item) {
				let type = ['nickname', 'gender', 'address', "motto"];
				uni.navigateTo({
					url: "/pages/setinfo/setinfo?type=" + type[index] + "&item=" + encodeURIComponent(JSON
						.stringify(item))
				})
			}
		}
	}
</script>

<style>
	.btn_bg {
		margin: 50rpx;
		width: 650rpx;
	}

	.userinfoBg {
		width: 710rpx;
		min-height: 90rpx;
		height: auto;
		margin-left: 20rpx;
		margin-top: 30rpx;
		display: flex;
		flex-direction: column;
		/* 垂直排列 */
	}

	.infobg {
		min-height: 90rpx;
		height: auto;
		/* 高度随内容自动扩展 */
		width: 710rpx;
		display: flex;
		align-items: center;
		/* 垂直居中 */
		line-height: 1.5;
		/* 行高优化阅读 */
	}

	.title {
		width: 340rpx;
		margin-left: 12rpx;
		color: #949494;
	}

	.info {
		width: 300rpx;
		margin-left: 12rpx;
		text-align: right;
	}

	.arrow {
		margin-left: 10rpx;
		width: 30rpx;
		height: 30rpx;
	}

	.line {
		width: 710rpx;
		background-color: #949494;
		height: 2rpx;
		margin-top: auto;
		margin-bottom: 1rpx;
	}
</style>